package com.coursehub.exercise.entity;

import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.time.LocalDateTime;

/**
 * 学生题目答案实体类
 */
@Getter
@Setter
@NoArgsConstructor
@Entity
@Table(name = "student_question_answer")
public class StudentQuestionAnswer {
    
    @EmbeddedId
    private StudentQuestionAnswerId id;
    
    @Column(name = "student_answer", length = 1000)
    private String studentAnswer;
    
    @Column(name = "is_correct")
    private Boolean isCorrect;
    
    @Column(name = "score_obtained")
    private Integer scoreObtained = 0;
    
    @Column(name = "answered_at")
    private LocalDateTime answeredAt;
    
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumns({
        @JoinColumn(name = "user_id", referencedColumnName = "user_id", nullable = false, insertable = false, updatable = false),
        @JoinColumn(name = "exercise_id", referencedColumnName = "exercise_id", nullable = false, insertable = false, updatable = false)
    })
    private StudentExercise studentExercise;
    
    @MapsId("questionId")
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "question_id", nullable = false, insertable = false, updatable = false)
    private Question question;
    
    public StudentQuestionAnswer(StudentQuestionAnswerId id, String studentAnswer, Question question) {
        this.id = id;
        this.studentAnswer = studentAnswer;
        this.question = question;
        this.answeredAt = LocalDateTime.now();
    }
    
    @PrePersist
    protected void onCreate() {
        if (answeredAt == null) {
            answeredAt = LocalDateTime.now();
        }
    }
}